package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList.ReplyInfo.ReContent;

public class YuErJingCommentDetailAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private int parentPosition = -1;
	private List<ReContent> replyList;
	OnClickListener huiFuOnclickLisenter;
	OnClickListener viewMoreOnclickListener;
	OnClickListener huiFuPingLunOnclickLisenter;
	TextView tianshu;
	TextView sex;
	String babyyears;
	String babysex;
	String uid;
	Context context;
	TextView huifu;
	TieZiBaoHanReply.GetAList model;

	public YuErJingCommentDetailAdapter(Context context,
			List<ReContent> replyList, int parentPosition,
			OnClickListener huiFuOnclickLisenter,
			OnClickListener huiFuPingLunOnclickLisenter,
			OnClickListener viewMoreOnclickListener) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.parentPosition = parentPosition;
		this.replyList = replyList;
		this.huiFuOnclickLisenter = huiFuOnclickLisenter;
		this.huiFuPingLunOnclickLisenter = huiFuPingLunOnclickLisenter;
		this.viewMoreOnclickListener = viewMoreOnclickListener;
	}

	@Override
	public int getCount() {
		int count = replyList.size();
		return replyList.size();
	}

	public void clearList() {
		this.replyList.clear();
	}

	public void updateList(List<ReContent> replyList) {
		this.replyList.addAll(replyList);
	}

	@Override
	public ReContent getItem(int position) {
		return replyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		uid = SPUtils.get(context, null, SpPublicParams.UID, "") + "";
		ViewHolder viewHolder = null;

		if (viewHolder == null) {
			convertView = inflater.inflate(R.layout.item_comment_reply, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_comment_reply_text = (TextView) convertView
					.findViewById(R.id.tv_msg_content);
			viewHolder.tv_comment_reply_writer = (TextView) convertView
					.findViewById(R.id.tv_msg_title);
			viewHolder.huifu = (TextView) convertView.findViewById(R.id.huifu);
			viewHolder.tv_comment_reply_writer2 = (TextView) convertView
					.findViewById(R.id.tv_comment_reply_writer2);
			viewHolder.content = (TextView) convertView
					.findViewById(R.id.content);
			viewHolder.head = (RoundImageView) convertView
					.findViewById(R.id.head);
			viewHolder.datetime = (TextView) convertView
					.findViewById(R.id.datetime);
			viewHolder.huifupinglun = (LinearLayout) convertView
					.findViewById(R.id.huifupinglun);
			viewHolder.tianshu = (TextView) convertView
					.findViewById(R.id.tianshu);
			viewHolder.sex = (TextView) convertView.findViewById(R.id.sex);
			viewHolder.more = (TextView) convertView.findViewById(R.id.more);
			viewHolder.huifu2 = (TextView) convertView
					.findViewById(R.id.huifu2);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ReContent reply = getItem(position);
		// if (!reply.getId().equals("0")) {// �ظ�����
		// viewHolder.huifu.setVisibility(View.VISIBLE);
		// viewHolder.tv_comment_reply_writer.setText(reply.getAuthor());
		// viewHolder.tv_comment_reply_writer2.setVisibility(View.VISIBLE);
		// viewHolder.tv_comment_reply_writer2.setText(reply.getToUinfo().getNickname());
		// }
		if (reply.getAuthorId().equals(uid)) {
			// viewHolder.huifu2.setText("删除");
			viewHolder.huifu2.setText("回复");
		} else {
			viewHolder.huifu2.setText("回复");
			viewHolder.huifu2.setTag(position);
			viewHolder.huifu2.setOnClickListener(huiFuOnclickLisenter);
		}
		viewHolder.huifupinglun.removeAllViews();
		if (!reply.getStatus().equals("-1")) {
			if (reply.getComments() != null) {// 二级回复
				for (int i = 0; i < reply.getComments().size(); i++) {
					View viewHuiFu = LayoutInflater.from(
							MainApplication.context).inflate(
							R.layout.huifupinglun, null);
					TextView huifupinglun_content = (TextView) viewHuiFu
							.findViewById(R.id.huifupinglun_content);
					TextView huifupinglun_writer = (TextView) viewHuiFu
							.findViewById(R.id.huifupinglun_writer);
					huifupinglun_content.setText(reply.getComments().get(i)
							.getComment());
					huifupinglun_writer.setText(reply.getComments().get(i)
							.getAuthor()
							+ " :");

					viewHuiFu.setTag(reply.getComments().get(i).getAuthorId());
					viewHuiFu.setTag(R.id.tag_first, position);
					viewHuiFu.setTag(R.id.tag_second, i);
					viewHuiFu.setOnClickListener(huiFuPingLunOnclickLisenter);
					viewHolder.huifupinglun.addView(viewHuiFu);
				}
				// viewHolder.huifu.setVisibility(View.GONE);
				// viewHolder.huifupinglun_content.setVisibility(View.GONE);
				// viewHolder.huifupinglun_writer.setText(reply.getAuthor() +
				// ":");
				if (reply.getComments().size() >= 3) {
					viewHolder.more.setVisibility(View.VISIBLE);
					viewHolder.more.setOnClickListener(viewMoreOnclickListener);
					viewHolder.more.setTag(position);
				} else {
					viewHolder.more.setVisibility(View.GONE);
				}
			} else {
				viewHolder.more.setVisibility(View.GONE);
				viewHolder.huifupinglun.setVisibility(View.GONE);
			}
			// viewHolder.tv_comment_reply_text.setText(reply.getComment());
			SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
			String datime = dateFormat.format(Long.parseLong(reply
					.getDateline()) * 1000L);
			viewHolder.datetime.setText(datime);

			viewHolder.content.setText(reply.getMessage());
			if (reply.getBabyDefault().getBirthday() != null) {
				viewHolder.sex
						.setText(babysex(reply.getBabyDefault().getSex()));
				viewHolder.tianshu.setText(babybirth(reply.getBabyDefault()
						.getBirthday()));
			} else {
				viewHolder.sex.setVisibility(View.GONE);
				viewHolder.tianshu.setVisibility(View.GONE);
			}

			// if (TextUtils.isEmpty(reply.get)) {
			viewHolder.tv_comment_reply_writer.setText(reply.getAuthor());
			// XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
			// + reply.getHead(), viewHolder.head);
			if (reply.getHead() == null || "".equals(reply.getHead())) {

				if ("1".equals(reply.getSex())) {
					viewHolder.head.setImageResource(R.drawable.male);
				}else {
					viewHolder.head.setImageResource(R.drawable.female);
				}
			} else {
				if (reply.getHead().contains("http")) {
					XImageLoader.load(reply.getHead(), viewHolder.head);
				} else {
					XImageLoader.load(GlobalConfig.API_URL
							+ "/picture/index?id=" + reply.getHead(),
							viewHolder.head);
				}
			}
			// } else {
			viewHolder.huifu.setVisibility(View.GONE);
			viewHolder.tv_comment_reply_writer2.setVisibility(View.GONE);
			int start = reply.getAuthor().length();
			String text = reply.getAuthor() + ":";
			SpannableStringBuilder ss = new SpannableStringBuilder(text);
			// ����ָ��λ�����ֵı�����ɫ�������ظ������óɻ�ɫ��
			ss.setSpan(new ForegroundColorSpan(Color.parseColor("#5A5A5A")),
					start, start + 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			viewHolder.tv_comment_reply_writer.setText(ss);
			// }
			// ��¼����ظ�ʱ����Ӧ��position,����ȷ�����ظ��Ķ��������¥��¥�������¼���ڵ��position
			// convertView.setTag(R.id.tag_first, parentPosition);
			// convertView.setTag(R.id.tag_second, position);
		} else {
			viewHolder.more.setVisibility(View.GONE);
		}
		return convertView;
	}

	public class ViewHolder {
		private TextView tv_comment_reply_writer, huifu, huifu2,
				tv_comment_reply_writer2, content, huifupinglun_content, more,
				huifupinglun_writer, datetime; // �������ǳ�
		private TextView tv_comment_reply_text; // ���� ����
		private RoundImageView head;
		LinearLayout huifupinglun;
		TextView tianshu;
		TextView sex;

	}

	String babysex(String sex) {
		if ("0".equals(sex)) {
			babysex = "女宝";
		} else if ("1".equals(sex)) {
			babysex = "男宝";

		} else {
			babysex = "孕育中";
		}

		return babysex;

	}

	String babybirth(String birth) {

		SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日");
		Calendar calendar = Calendar.getInstance();
		int yearNow = calendar.get(Calendar.YEAR);
		int monthNow = calendar.get(Calendar.MONTH);
		int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----", yearNow + "monthNow-----" + "-------dayBirth"
				+ monthNow + "n" + dayNow);
		Calendar calendar2 = Calendar.getInstance();
		long timews = Long.valueOf(birth);
		Date date = new Date(timews * 1000L);
		calendar2.setTime(date);
		// Log.d("hu", date.toString());
		int yearBirth = calendar2.get(Calendar.YEAR);
		int monthBirth = calendar2.get(Calendar.MONTH);
		int dayBirth = calendar2.get(Calendar.DAY_OF_MONTH);
		Log.d("hu-----2", yearBirth + "monthBirth-----" + "-------dayBirth"
				+ monthBirth + "n" + dayBirth);
		int age = yearNow - yearBirth;
		int month = monthNow - monthBirth;
		int day = dayNow - dayBirth;
		if (age > 0) {
			if (month > 0) {
				if (day < 0) {
					month--;
					age--;
				}

			} else if (month < 0) {
				month += 12;
				if (day < 0) {
					month--;
					age--;
				}
			}

		} else {
			if (day < 0) {
				month--;
			}

		}
		SimpleDateFormat myFmt5 = new SimpleDateFormat("d");
		String birthday2 = myFmt1.format(Long.parseLong(birth) * 1000L);
		String birthday = myFmt5.format(System.currentTimeMillis()
				- Long.parseLong((birth) + "") * 1000L);
		int days = Integer.parseInt(birthday) % 30;
		Log.d("hu-bir", birthday2);
		Log.d("hu-old", (age + "岁" + month + "月" + days + "天"));
		if (day == 0) {
			babyyears = age + "岁" + month + "月" + day + "天";
		} else {
			babyyears = age + "岁" + month + "月" + days + "天";
		}

		return babyyears;
	}
}
